package com.yunhe.option.controller.base;

import com.alibaba.excel.EasyExcel;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import com.yunhe.option.domain.base.Enum;
import com.yunhe.option.domain.base.ImportEnumItems;
import com.yunhe.option.listener.EnumItemsListener;
import com.yunhe.option.service.base.EnumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EnumController
 * @Description TODO
 * @Author HeTao
 * @Date 2021/2/26 14:19
 * @Version 1.0
 **/
@Api(value = "EnumController", description = "枚举", tags = {"Base_Enum"})
@RestController
@RequestMapping("/enum")
public class EnumController {

    @Autowired
    private EnumService enumService;

    /**
     * 新增枚举
     * @param enums 枚举对象
     */
    @ApiOperation("新增枚举")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> post(@RequestBody Enum enums)
            throws ArgumentErrorException, UniqueConstraintsException {
        Enum newEnum = enumService.createEnum(enums);
        return new ResponseEntity<>(new ResultObject<>(newEnum), HttpStatus.OK);
    }

    @ApiOperation("编辑枚举")
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> updateEnum(@PathVariable("id") Long id,
                                                   @RequestBody Enum enums)
            throws ArgumentErrorException, UniqueConstraintsException {
        enums.setId(id);
        Enum newEnum = enumService.updateEnum(enums);
        return new ResponseEntity<>(new ResultObject<>(newEnum), HttpStatus.OK);
    }

    @ApiOperation("删除枚举")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id)
            throws ArgumentErrorException, UniqueConstraintsException {
        enumService.delete(id);
        return new ResponseEntity<>(new ResultObject<>(), HttpStatus.OK);
    }

    @ApiOperation("查看枚举详情")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getEnumById(@PathVariable("id") Long id) throws ArgumentErrorException {
        Enum enums = enumService.getEnumById(id);
        return new ResponseEntity<>(new ResultObject<>(enums), HttpStatus.OK);
    }

    @ApiOperation("查看字典类型列表")
    @RequestMapping(value = "/getEnumByCondition",method = RequestMethod.GET)
    public ResponseEntity<ResultObject> getEnumByCondition(@ModelAttribute PageParam pageParam) throws ArgumentErrorException {
        Iterable<Enum> enums = enumService.getEnumByCondition(pageParam);
        return new ResponseEntity<>(new ResultObject<>(enums), HttpStatus.OK);
    }

    @ApiOperation("数据字典导入")
    @RequestMapping(value = "/importEnumAndItems",method = RequestMethod.POST)
    public ResponseEntity<ResultObject> importEnumAndItems(@RequestParam("file") MultipartFile file) throws ArgumentErrorException, IOException {
        EnumItemsListener ajglListener = new EnumItemsListener(enumService);
        //这里的sheet里面放的是sheet的表名，和表名一样的才能导入，也可以是其他的，进去看源码就行
        EasyExcel.read(file.getInputStream(), ImportEnumItems.class, ajglListener).sheet("枚举条目导入").doReadSync();
        Map<String, Object> hashMap = new HashMap<>();
        List<String> errMessage = ajglListener.getErrMessage();
        if (errMessage.isEmpty()) {
            hashMap.put("success", true);
        } else {
            hashMap.put("success", false);
            hashMap.put("errMessage", errMessage);
        }
        return new ResponseEntity<>(new ResultObject<>(hashMap), HttpStatus.OK);
    }
}
